package cn.schoolwow.util.module.check.instanceList.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instance.QueryInstance;

import java.util.List;

public class CheckInstanceListIdFieldFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String idFieldName = (String) flowContext.getData("idFieldName");
        List list = (List) flowContext.checkData("list");
        String message = (String) flowContext.checkData("message");

        if(null==idFieldName||idFieldName.isEmpty()){
            throw new IllegalArgumentException(message+"的id字段名称不能为空");
        }

        for(Object instance:list){
            Object fieldValue = QueryInstance.newQuery(instance)
                    .execute()
                    .getSingleFieldValue(idFieldName);
            if(null==fieldValue){
                throw new IllegalArgumentException(message+"的字段值为空!");
            }
            if(fieldValue instanceof Integer){
                Integer integer = (Integer) fieldValue;
                if(integer<=0){
                    throw new IllegalArgumentException(message+"的字段值小于0!当前值:"+integer);
                }
            }else if(fieldValue instanceof Long){
                Long aLong = (Long) fieldValue;
                if(aLong<=0){
                    throw new IllegalArgumentException(message+"的字段值小于0!当前值:"+aLong);
                }
            }else{
                throw new IllegalArgumentException(message+"的字段类型不为整型!当前类型:"+fieldValue.getClass().getName());
            }
        }
    }

    @Override
    public String name() {
        return "实例列表-检查id字段列是否大于0";
    }
}
